package org.itboys.ace.web.lasted.shiro;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.itboys.admin.common.SystemLoginUserPermission;
import org.itboys.admin.entity.lasted.SystemUser;
import org.itboys.admin.service.lasted.SystemRolePermissionService;
import org.itboys.admin.service.lasted.SystemUserRoleService;
import org.itboys.admin.service.lasted.SystemUserService;
import org.itboys.admin.tools.AdminCookieHolder;
import org.itboys.commons.utils.encryption.Digests;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.entity.mysql.SundayOutStoreOrder;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * 作者：zzq
 * 时间：2018/8/30
 * 描述：
 */
public class AceShiroRealm extends AuthorizingRealm {

    {
        super.setName("AceShiroRealm");
        super.setAuthorizationCachingEnabled(false);
    }

    /**
     * 用于检测角色和权限
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        String userName = (String) principals.getPrimaryPrincipal();
        //从缓存中获取角色或者权限数据
        //Set<String> roles = getRolesByUserName(userName);
        //simpleAuthorizationInfo.setRoles(roles);
        Set<String> permissions = getPermissionByUserName();
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.setStringPermissions(permissions);
        return simpleAuthorizationInfo;
    }


    /**
     * 用于认证和登录
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //1,从主体传过来的认证信息中，获取用户名
        //因已经验证过用户信息  所以不重复验证
        String userName = (String) token.getPrincipal();
        String password = AdminCookieHolder.getAdminPassword();
        if (password == null) {
            return null;
        }
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(userName, password, "AceShiroRealm");
        return authenticationInfo;
    }
    private Set<String> getPermissionByUserName() {
        List<SystemLoginUserPermission>permissions=SystemLoginUserPermission.loginUserOperatePermssion;
        Set<String> sets = new HashSet<>();
        for (SystemLoginUserPermission systemLoginUserPermission :permissions) {
            sets.add(systemLoginUserPermission.getUrl());
        }
        return sets;
    }
    /**
     * 模拟数据库查询
     * @param userName
     * @return
     */
  /*  public String getPasswordByUserName(String userName) {
        String password="";
        return password;
    }

    public Set<String> getRolesByUserName(String userName) {
        Set<String> sets = new HashSet<>();
        sets.add("admin");
        sets.add("user");
        return sets;
    }

   */
}
